package com.gitee.wsl.compose.ui.lazy.tree

import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.type
import com.gitee.wsl.compose.ui.lazy.DefaultSelectableLazyColumnKeyActions
import com.gitee.wsl.compose.ui.lazy.SelectableLazyListKey
import com.gitee.wsl.compose.ui.lazy.SelectableLazyListState
import com.gitee.wsl.compose.ui.lazy.SelectionMode


fun DefaultTreeViewKeyActions(treeState: TreeState): DefaultTreeViewKeyActions {
    val keybindings =
        when {
            //hostOs.isMacOS -> DefaultMacOsTreeColumnKeybindings
            else -> DefaultTreeViewKeybindings
        }
    return DefaultTreeViewKeyActions(keybindings, DefaultTreeViewOnKeyEvent(keybindings, treeState))
}

 class DefaultTreeViewKeyActions(
    override val keybindings: TreeViewKeybindings,
    override val actions: DefaultTreeViewOnKeyEvent,
) : DefaultSelectableLazyColumnKeyActions(keybindings, actions) {
    override fun handleOnKeyEvent(
        event: KeyEvent,
        keys: List<SelectableLazyListKey>,
        state: SelectableLazyListState,
        selectionMode: SelectionMode,
    ): KeyEvent.() -> Boolean = lambda@{
        if (type == KeyEventType.KeyUp) return@lambda false
        val keyEvent = this
        with(keybindings) {
            with(actions) {
                if (selectionMode == SelectionMode.None) return@lambda false
                when {
                    isSelectParent -> onSelectParent(keys, state)
                    isSelectChild -> onSelectChild(keys, state)
                    super.handleOnKeyEvent(event, keys, state, selectionMode).invoke(keyEvent) -> return@lambda true

                    else -> return@lambda false
                }
            }
        }
        return@lambda true
    }
}
